package CampaignLevels;

import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.ButtonSprite;

import nl.hro.mgdd.team4.raidersofhexagoniland.Scenes.BattleSetupScene;
import nl.hro.mgdd.team4.raidersofhexagoniland.Scenes.LevelBaseScene;
import nl.hro.mgdd.team4.raidersofhexagoniland.Scenes.SceneManager;
import Parts.HexagonTile;
import Parts.HexagonTile.HexagonState;
import Parts.Player;
import Units.Unit;
import Utils.Vector2;

public class Level1 extends LevelBaseScene {

	public Level1() {
		SceneManager._player = new Player(8, 6, 7);
		origin = new Vector2(0f, 22f);

		map = new HexagonTile[7][5];

		map[0][3] = new HexagonTile(new Vector2(0, 3), HexagonState.StartTile);

		map[1][3] = new HexagonTile(new Vector2(1, 3), HexagonState.Occupied);
		map[1][2] = new HexagonTile(new Vector2(1, 2), HexagonState.Occupied);

		map[2][4] = new HexagonTile(new Vector2(2, 4), HexagonState.Occupied);
		map[2][2] = new HexagonTile(new Vector2(2, 2), HexagonState.Occupied);

		map[3][4] = new HexagonTile(new Vector2(3, 4), HexagonState.Occupied);
		map[3][3] = new HexagonTile(new Vector2(3, 3), HexagonState.Occupied);
		map[3][2] = new HexagonTile(new Vector2(3, 2), HexagonState.Occupied);

		map[4][3] = new HexagonTile(new Vector2(4, 3), HexagonState.Occupied);
		map[4][2] = new HexagonTile(new Vector2(4, 2), HexagonState.Occupied);

		map[5][1] = new HexagonTile(new Vector2(5, 3), HexagonState.Occupied);

		map[6][3] = new HexagonTile(new Vector2(6, 3), HexagonState.FinishTile);

		int eSword = 0;
		int eSpear = 0;
		int eBow = 0;
		for (int x = 0; x < 7; x++)
			for (int y = 0; y < 5; y++)
				if (map[x][y] != null) {
					map[x][y].setTag(10 * x + y);
					for (Unit u : map[x][y].getUnits()) {
						if (u.getTag() == 999)
							eBow++;
						if (u.getTag() == 998)
							eSpear++;
						if (u.getTag() == 997)
							eSword++;
					}
					tiles.add(map[x][y]);
				}

		SceneManager._enemy = new Player(eBow, eSword, eSpear);
		
		this.setBackground(new Background(0.5f, 0.5f, 0.5f));
		loadResources();
	}

	@Override
	public void onClick(final ButtonSprite pButtonSprite,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		HexagonTile selectedTile = null;
		for (HexagonTile h : tiles) {
			if (h.getTag() == pButtonSprite.getTag()) {
				selectedTile = h;
				break;
			}
		}
		if (selectedTile == null)
			return;
		if (selectedTile.GetState() == HexagonState.Free
				|| selectedTile.GetState() == HexagonState.StartTile)
			return;
		if (canTileBeAttacked(selectedTile)) {
			BattleSetupScene bss = new BattleSetupScene(selectedTile);
			this.setChildScene(bss);
		} else
			return;
	}
}
